'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:UserdefinedUserdefinedCtrl
 * @description
 * # UserdefinedUserdefinedCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('UserdefinedUserdefinedCtrl', function($scope, $rootScope, DataService, openApi, Util, $location, $routeParams, FormSubmit) {
        var selectData, params;
        $scope.param = {}
            // --------------------------------------
        $scope.sheetObj = {};
        // 底部关联模块类型选框添加
        var relModuleObj = {};
        relModuleObj.sheettitle = "关联模块";
        relModuleObj.callback = function(item) {
            if (item.selectTabContent == "无") {
                $scope.param.relatedModuleName = "无";
                $scope.param.relatedModule = "";
                $scope.param.moduleNo = "";
                return;
            }
            $scope.param.relatedModule = item.selectTab;
            $scope.param.relatedModuleName = item.selectTabContent;
            $scope.param.moduleNo = ""
        }
        $scope.sheetObj.relModuleObj = relModuleObj;
        $scope.showrelModule = function() {
            Util.getModuleRel(Util.CADUIT_MODULE, function(data) {
                $scope.sheetObj.relModuleObj.tabList = data.data
                $scope.showSheet("relModuleObj")
            })

        };
        $scope.showSheet = function(type) {
            var sheetObj = $scope.sheetObj[type]
            $scope.sheetObj.type = type
            $scope.sheetObj.tabList = sheetObj.tabList
            $scope.sheetObj.sheettitle = sheetObj.sheettitle
            $scope.sheetObj.ifshowSheet = true
            $rootScope.showMasker = true
        };
        // 
        $scope.choosepeople = function() {
            Util.savaParamForReview($scope.param, $routeParams.id);
            // 里面包含了title
            $location.path("userdefined/choosePerson")
        }
        $scope.chooseinfo = function() {
            // $routeParams.id通过id确定唯一回显id
            if (!$scope.param.relatedModule) {
                return
            }
            Util.savaParamForReview($scope.param, $routeParams.id);
            // 里面包含了title
            var relModuleObj = {
                name: "UserdefinedUserdefinedCtrl",
                selectTab: $scope.param.relatedModule,
                selectTabContent: $scope.param.relatedModuleName
            };
            Util.savaDataforsearch(relModuleObj);
            $location.path("finance/choosepeople")

        };

        var getNames = function() {
            var data = []
            $scope.param.users.forEach(function(item, index) {
                data.push(item.name);
            })
            return data.join();
        }
        var getuserIds = function() {
            var data = []
            $scope.param.users.forEach(function(item, index) {
                data.push(item.userId);
            })
            return data.join();
        };
        // 提交申请
        $scope.submit = function() {
            if ($scope.param.users.length < 1) {
                Util.Toast("请填选择审批人员");
                return;
            }
            if (!$scope.validation.passvalidation) {
                Util.Toast($scope.validation.tip);
                return;
            }
            $scope.param.aduitUserNames = getNames();
            $scope.param.aduitUserIds = getuserIds();
            // 提交
            var urlParam = "?operationId=-2" + "&&processId=" + $routeParams.id + "&&url=" + openApi.caduitObject;
            var options = {};
            var myDest = {};
            options.urlParam = urlParam;
            options.param = angular.copy($scope.param, myDest);
            var TVtype;
            TVtype = Util.getTVType(); //根据页面的来源来判断是发起的流程还是修改的流程，如果是发起的则不管，如果是修改的则获取数据用来回显
            // 如果是修改页面的话则提交的地址不一样，参数一样
            // 不删除会失败
            delete options.param.users
            if (TVtype == Util.UPDATE) {
                options.url = openApi.ip + openApi.caduitObject
            }
            new FormSubmit(options);
        };
        $scope.delpersons = function(index) {
            $scope.param.users.splice(index, 1);
        };
        var init = function() {
            // 获取回显的详细信息
            Util.getDetailData($routeParams.id, function(data) {
                $scope.param = data;
                var SPperson = Util.getLocalInfo("SPperson");
                // 获取选择页面返回的数据并且合并到参数中
                if (Array.isArray(SPperson) && SPperson.length > 0) {
                    $scope.param.users = SPperson
                } else {
                    $scope.param.users = $scope.param.users || [];
                }

            });
        };
        // 初始化
        init();
        // 显示审批记录判断
        Util.showloglist($routeParams.id, function(data) {
            $scope.logData = data;
        });
    });
